<template>
	<view class="my-activity-list" :class="[config.style]">
		<!-- #ifdef H5-->
		<Top :config="config" />
		<!-- #endif -->
		<view class="main-section tabbar-bottom top">
			<!-- 顶部固定部分 -->
			<view class="top-warp">
				<view class="tabs-block">
					<view class="tabs">
						<ListTabs ref="tabsRef" v-model="tabIndex" :config="config" :tabs="tabs" @change="tabChange"></ListTabs>
					</view>
				</view>
			</view>
			<!--数据列表部分-->
			<view class="list-section uni-clearfix">
				<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
				<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="mescrollTop" :down="downOption" @down="downCallback" @up="upCallback">
					<!-- 数据列表 -->
					<view class="list">
						<view class="item" :id="'swiper_item_' + index" v-for="(item, index) in list" :key="index">
							<view class="content" @click="goToDetailLink(item.products_id)">
								<view class="cover">
									<image mode="widthFix" :src="item.cover_400" @error="imageError" class="image"></image>
								</view>
								<view class="info">
									<view class="title text-ellipsis-2">{{item.title}}</view>
									<view class="description text-ellipsis">{{item.description}}</view>
									
									<view class="start-time">
										<text class="title">开始时间：</text>
										<text>{{item.start_time_str}}</text>	
									</view>
									<view class="end-time">
										<text class="title">结束时间：</text>
										<text>{{item.end_time_str}}</text>	
									</view>
								</view>
							</view>
							
							<view class="action-info">
								<!--未开始-->
								<view class="item" v-if="item.activity_status == 1">
									<view class="action-btn status0-btn" v-if="item.status == 0">
										<button type="default" size="mini" @click="verification(item.id,item.products_id)">出示核销码</button>
									</view>
									<view class="action-btn status1-btn" v-if="item.status == 1">
										<button type="default" size="mini" @click="verification(item.id,item.products_id)">核销详情</button>
									</view>
								</view>
								<!--进行中-->
								<view class="item" v-if="item.activity_status == 2">
									<view class="action-btn status0-btn" v-if="item.status == 0">
										<button type="default" size="mini" @click="verification(item.id,item.products_id)">出示核销码</button>
									</view>
									<view class="action-btn status1-btn" v-if="item.status == 1">
										<button type="default" size="mini" @click="verification(item.id,item.products_id)">核销详情</button>
									</view>
								</view>
								<!--已结束-->
								<view class="item" v-if="item.activity_status == 3">
									
								</view>
							</view>
						</view>	
					</view>
				</mescroll-uni>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	import ListTabs from "../../common/listTabs" //列表tab
	import Top from '../../common/top.vue'
	
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			ListTabs,
			Top
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				page: 1, //当前页码
				list: [], //列表数据
				tabs: ['全部','未开始','进行中','已结束'],
				tabIndex: 0 ,// tab下标
				activityStatus: 'all', // 默认全部活动 all:所有 1：未开始 2：进行中 3：已结束
				mescrollTop: 74,
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
				swiperHeight: 200, //当前swiper高度
			}
		},
		onLoad(options) {
			//获取url参数
			this.urlParams = options
			console.log(this.urlParams,'urlparams')
		},
		created() {
			// #ifdef H5
			console.log(this.urlParams.tab_index,'tab_index')
			this.mescrollTop = 148
			// #endif
			
			//渲染结束后，判断上一页是否更改tabindex参数
			if(this.urlParams.tab_index){
				this.list = []// 先置空列表,显示加载进度
				this.$nextTick(() => {
					this.$refs.tabsRef.tabClick(Number(this.urlParams.tab_index))
				})
			}
		},
		computed: {
			...mapState(['config'])
		},
		methods: {

			//上拉加载的回调
			upCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				console.log(keyword)
				let data = {}
					data.action = 'my'
					data.page = this.page
					data.activity_status = this.activityStatus
				//获取首页数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/activity'
					//#endif
					//#ifdef H5
					url = 'entry/site/activity'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				this.page = 1
				
				let data = {}
					data.action = 'my'
					data.page = this.page
					data.activity_status = this.activityStatus
				//获取首页数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/activity'
					//#endif
					//#ifdef H5
					url = 'entry/site/activity'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							//清空列表
							that.list = []
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			
			/**
			 * 进入商品详情
			 * @param {Object} id
			 * @param {Object} type
			 */
			goToDetailLink: function(id){
				let urlParams = 'id='+id
				let page = '../../activity/detail'
				
				let url = page + '?' + urlParams
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
			
			/**
			 * 切换TAB
			 * @param {Object} index
			 * all:所有 0：未开始 1：进行中 2：已结束 3
			 */
			tabChange(index) {
				console.log(this.urlParams)
				this.page = 1
				this.list = []// 先置空列表,显示加载进度
				if(index == 0){
					this.activityStatus = 'all'
				}
				if(index == 1){
					this.activityStatus = '1'
				}
				if(index == 2){
					this.activityStatus = '2'
				}
				if(index == 3){
					this.activityStatus = '3'
				}
				
				//console.log(this.status)
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			
			/**
			 * 进入核销页
			 * @param {Object} id
			 */
			verification(id,products_id){
				let urlParams = 'id='+id+'&products_id='+products_id
				let url = 'verification?' + urlParams
				//console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			margin: 0 20rpx;
			.tabs {
				flex: 1;
			}
		}
	}
	.list {
		/*小图*/
		padding: 0 10rpx;
		display: block;

		.item {
			box-sizing:border-box;
			background-color: #fff;
			margin: 10rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.02);
			overflow: hidden;
			.base-info {
				display: flex;
				font-size: 28rpx;
				border-bottom: 2rpx solid #eee;
				line-height: 64rpx;
				color: #666;
				padding: 0 20rpx;
			}
			.content {
				display: flex;
				padding: 20rpx;
				.cover {
					width: 280rpx;
					overflow: hidden;
					.image {
						border-radius: 10rpx;
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					flex: 1;
					padding: 0 0 0 20rpx;
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.start_time {
						margin-top: 20rpx;
					}
					.start-time,.end-time {
						font-size: 24rpx;
						color: #999;
						.title {
							font-size: 24rpx;
							color: #666;
						}
					}
				}
			}
			.other-info {
				padding: 20rpx;
				text-align: right;
				font-size: 24rpx;
				color: #666;
				.num {
					color: #000;
				}
				.price {
					color: #000;
				}
			}
			.action-info {
				border-top: 2rpx solid #eee;
				color: #666;
				padding: 0 10rpx 0 10rpx;
				.item{
					font-size: 28rpx;
					display: flex;
					flex-direction: row-reverse;
					.action-btn {
						padding: 10rpx;
						button {
							display: block;
							color: #fff;
							&::after {
								border: 0;
							}
						}
					}
					.action-btn.status0-btn {
						
					}
					.action-btn.status1-btn {
						button {
							display: block;
							background-color: #eee;
							color: #666;
							&::after {
								border: 0;
							}
						}
					}
					.confirm-btn {
						button {
							
						}
					}
				}
			}
		}
	}
	
	.Blue {
		.price {
			color: $Blue;
		}
		.paid-price {
			color: $Blue;
		}
		.status0-btn button{
			background-color: $Blue;
		}
		.confirm-btn button{
			background-color: $Blue;
		}
	}
	.Green {
		.price {
			color: $Green;
		}
		.paid-price {
			color: $Green;
		}
		.status0-btn button{
			background-color: $Green;
		}
		.confirm-btn button{
			background-color: $Green;
		}
	}
	.LightRed {
		.price {
			color: $LightRed;
		}
		.paid-price {
			color: $LightRed;
		}
		.status0-btn button {
			background-color: $LightRed;
		}
		.confirm-btn button{
			background-color: $LightRed;
		}
	}
	.Orange {
		.price {
			color: $Orange;
		}
		.paid-price {
			color: $Orange;
		}
		.status0-btn button {
			background-color: $Orange;
		}
		.confirm-btn button{
			background-color: $Orange;
		}
	}
	.LightPink {
		.price {
			color: $LightPink;
		}
		.paid-price {
			color: $LightPink;
		}
		.status0-btn button {
			background-color: $LightPink;
		}
		.confirm-btn button{
			background-color: $LightPink;
		}
	}
	.Magenta {
		.price {
			color: $Magenta;
		}
		.paid-price {
			color: $Magenta;
		}
		.status0-btn button {
			background-color: $Magenta;
		}
		.confirm-btn button{
			background-color: $Magenta;
		}
	}
</style>
